Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve specified type in comparisons #390

Merged
merged 4 commits into from
Jan 4, 2021

Conversation

jlherren
Copy link
Contributor

Note that this PR has a side-effect that might need to be addressed, consider this code:

$array = rand() === 0 ? hash_algos() : false;
for ($i = 0; $i < count($array); $i++) {
	echo $array[$i] . "\n"; // <-- this line
}

The change in output is as follows:

- Cannot access offset int on array|false.
+ Cannot access offset int<min, 9223372036854775806> on array|false.

While technically correct, this is not very pretty.

Perhaps this can be fixed by adjusting how IntegerRangeType is represented. Currently, int<0, max> (return value of count()) is internally represented by min=0, max=PHP_INT_MAX, which is technically a bounded interval. If it were to use min=0, max=null instead, then the intended meaning of an open interval can be represented more accurately and the new code in this PR could make use of that information. If this sounds good I'll have a go at it.

@ondrejmirtes
Copy link
Member

Yes, please do the min=0, max=null refactoring first. We should honor the usual math rules that INF minus 1 is still INF :) So int<min, 9223372036854775806> should really be just an int.

@jlherren
Copy link
Contributor Author

I rebased this branch and the issue I mentioned is now fixed.

@ondrejmirtes
Copy link
Member

Linking the fixed issue: phpstan/phpstan#577

I'm gonna review this once the time allows, hopefull soon :)

@ondrejmirtes
Copy link
Member

Yes, please do. Can you also take a look at #416 - does it make sense to you?

@ondrejmirtes
Copy link
Member

Thank you!

@ondrejmirtes ondrejmirtes merged commit 377e49c into phpstan:master Jan 4, 2021
@jlherren jlherren deleted the improve-comparisons branch January 5, 2021 07:56
@jlherren jlherren restored the improve-comparisons branch January 5, 2021 07:57
@jlherren jlherren deleted the improve-comparisons branch January 5, 2021 07:57
@jlherren jlherren restored the improve-comparisons branch January 5, 2021 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants